using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Shell;

using BlueGameComponent;

namespace AirForce
{
    /// <summary>
    /// 这是游戏的主类型
    /// </summary>
    public class AirForceGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        ScreenManager screenManager;

        public AirForceGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Windows Phone 的默认帧速率为 30 fps。
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // 延长锁定时的电池寿命。
            InactiveSleepTime = TimeSpan.FromSeconds(1);

            graphics.IsFullScreen = true;
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;

            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            PhoneApplicationService.Current.Deactivated += GameDeactivated;
        }

        /// <summary>
        /// 允许游戏在开始运行之前执行其所需的任何初始化。
        /// 游戏能够在此时查询任何所需服务并加载任何非图形
        /// 相关的内容。调用 base.Initialize 将枚举所有组件
        /// 并对其进行初始化。 
        /// </summary>
        protected override void Initialize()
        {
            // TODO: 在此处添加初始化逻辑
            AudioManager.Initialize(this);

            //screenManager.AddScreen(new MainMenuScreen(), null);
            screenManager.AddScreen(new LogoScreen(), null);
            
            HighScoreScreen.LoadHighScore();

            base.Initialize();
        }

        /// <summary>
        /// 对于每个游戏会调用一次 LoadContent，
        /// 用于加载所有内容。
        /// </summary>
        protected override void LoadContent()
        {
            // 创建新的 SpriteBatch，可将其用于绘制纹理。
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D blanktexture = Content.Load<Texture2D>("Textures/blank");
            SpriteFont font = Content.Load<SpriteFont>("Fonts/MenuFont");
            screenManager.SetAsset(blanktexture,font);
            // TODO: 在此处使用 this.Content 加载游戏内容
        }

        /// <summary>
        /// 对于每个游戏会调用一次 UnloadContent，
        /// 用于取消加载所有内容。
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: 在此处取消加载任何非 ContentManager 内容
        }



        /// <summary>
        /// 当游戏该进行自我绘制时调用此项。
        /// </summary>
        /// <param name="gameTime">提供计时值的快照。</param>
        protected override void Draw(GameTime gameTime)
        {
           //GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: 在此处添加绘图代码

            base.Draw(gameTime);
        }
        private void GameDeactivated(object sender, DeactivatedEventArgs e)
        {
            foreach(BaseScreen screen in screenManager.GetScreens())
            {
                if (screen is GameScreen)
                {
                    if (!(screen as GameScreen ).Pause)
                    {
                        (screen as GameScreen).PauseGame();
                        break;
                    }
                }
            }
        }  
    }
}
